<template>
  <el-popover ref="popoverRef" trigger="click" placement="bottom-start" popper-class="p0" :width="240" @show="showList" @hide="hideList">
    <template #reference>
      <div class="search-item" :class="{ active: visible, isSelect: !!selectText }" @click="showList">
        <span class="label">{{ formItem.props?.label }}<span v-if="selectText">:</span></span>
        <span class="text">{{ selectText }}</span>
        <el-icon class="array-icon" :size="10" color="#999">
          <svg-icon name="ep:search" />
        </el-icon>
        <el-icon class="delete-icon" :size="10" color="#999" @click.stop="clear">
          <svg-icon name="ep:circle-close-filled" />
        </el-icon>
      </div>
    </template>

    <div class="select-list-wrap">
      <div class="search-wrap">
        <input v-model="keyword" type="text" placeholder="请输入" @keydown.enter="search" @input="changeInput($event)">
        <span class="sep ml3 mr3">-</span>
        <input v-model="keyword2" type="text" placeholder="请输入" @keydown.enter="search" @input="changeInput($event)">
        <span v-if="formItem.afterSuffix" class="font12 ml3">{{ formItem.afterSuffix }}</span>
      </div>

      <div class="select-footer">
        <el-button :auto-insert-space="false" type="primary" size="small" style="height: 28px;" @click="search">确定</el-button>
      </div>
    </div>
  </el-popover>
</template>

<script setup>
import { computed, watch } from 'vue'

const props = defineProps({
  filed1: [String, Number, Array],
  filed2: [String, Number, Array],
  attr: {
    type: Object,
    default: () => {
      return { placeholder: '请选择' }
    }
  },
  formItem: {
    type: Object
  }
})
const emit = defineEmits(['update:filed1', 'update:filed2', 'search'])
const visible = ref(false)
const popoverRef = ref(null)
const selected = ref(props.attr.multiple ? [] : null)
const keyword = ref('')
const keyword2 = ref('')
const selectText = ref('')

watch(() => props.filed1, () => {
  keyword.value = props.filed1
  if (keyword.value && keyword2.value) {
    selectText.value = `${keyword.value} - ${keyword2.value}`
  } else {
    selectText.value = keyword.value || keyword2.value
  }
})

watch(() => props.filed2, () => {
  keyword2.value = props.filed2
  if (keyword.value && keyword2.value) {
    selectText.value = `${keyword.value} - ${keyword2.value}`
  } else {
    selectText.value = keyword.value || keyword2.value
  }
})

function changeInput(e) {
  if (props.attr.step) {
    e.target.value = parseFloat(e.target.value).toFixed(props.attr.precision)
    console.log(e.target.value)
  }
}

function showList() {
  visible.value = true
}

function hideList() {
  visible.value = false
}

function search() {
  selected.value = keyword.value
  emit('update:filed1', keyword.value)
  emit('update:filed2', keyword2.value)
  if (keyword.value && keyword2.value) {
    selectText.value = `${keyword.value} - ${keyword2.value}`
  } else {
    selectText.value = keyword.value || keyword2.value
  }
  emit('search')
  visible.value = false
  popoverRef.value.hide()
}

function clear() {
  selected.value = props.attr.multiple ? [] : null
  keyword.value = ''
  keyword2.value = ''
  emit('update:filed1', '')
  emit('update:filed2', '')
  emit('search')
}
</script>

<style scoped lang="scss">
@import "./search.scss";
.search-wrap {
  margin: 10px 10px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  input {
    box-shadow: none;
    border: none;
    outline: none;
    flex: 1;
    padding: 5px;
    font-size: var(--font-size-min);
    width: 100px;
  }
}
</style>
